﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using weikebuluo.DAL;
using weikebuluo.Model;
using weikebuluo.Common;

namespace weikeweixin.web.admin.website
{
    public partial class template : weikeweixin.web.admin.cs.adminbase
    {
        t_template_dal cTemplateDAL = new t_template_dal();
        t_template_model cTemplateModel = new t_template_model();
        t_set_dal cMerchantDAL = new t_set_dal();
        t_set_model cMerchantModel = new t_set_model();
        public string Template;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user_id))
            {
                Response.Write("<script Language=\"Javascript\">alert(\"请先配置微官网\")");
                Response.Write("</script>");
                Response.End();
            }
            DataTable dtMerchant = cMerchantDAL.GetList("user_id = '" + user_id + "'").Tables[0];
            if (dtMerchant.Rows.Count > 0)
            {
                Template = dtMerchant.Rows[0]["Template"].ToString();
            }
            else
            {
                Template = "0";
            }

            switch (Request.QueryString["action"])
            {
                case "templatelist"://提交信息
                    GetTemplate();
                    break;
                case "bindtemp"://保存模板
                    SaveTemplate();
                    break;
            }
        }

        /// <summary>
        /// 适用模板
        /// </summary>
        public void GetTemplate()
        {
            DataTable cTemplate = cTemplateDAL.GetList("TStatus = 1").Tables[0];
            JsonOperate JsonOperate = new JsonOperate();
            Response.Write(JsonOperate.TableToBasicJson(cTemplate));
            Response.End();
        }

        /// <summary>
        /// 保存模板
        /// </summary>
        public void SaveTemplate()
        {
            DataTable dtMerchant = cMerchantDAL.GetList("user_id = '" + user_id + "'").Tables[0];
            cMerchantModel = cMerchantDAL.GetModel(Convert.ToInt32(dtMerchant.Rows[0]["MId"]));
            string Template = Request.Form["value"].Trim();
            cMerchantModel.Template = Convert.ToInt32(Template);
            cMerchantDAL.Update(cMerchantModel);
            Response.Write("{\"code\":\"200\"}");
            Response.End();
        }
    }
}